<template>
  <div class="yxp">
    <div class="yxp-wordList-top">
      <div class="back" @click="handleBack">
        <span></span>
      </div>
      <div class="yxp-top-c">
        <span
          v-for="(item, i) in tab"
          :key="i"
          :class="{ active: tabIndex === i }"
          @click="handleTab(i)"
          >{{ item }}</span
        >
      </div>
      <div class="yxp-top-r" v-if="(type === 'complete' || type === 'done')">
        <div
          class="start-btn"
          v-if="checkIndex === 0"
          @click="handleStartCheck"
        >
          {{ text }}
        </div>
        <div class="check-btn">
          <span
            v-for="(item, i) in check"
            :key="i + 'i'"
            :class="{ active: checkIndex === i }"
            @click="handleCheck(i)"
            >{{ item }}</span
          >
        </div>
      </div>
    </div>
    <div class="yxp-word-box" id="wordBox" v-if="!isStudy">
      <div class="yxp-wordList-box">
        <div class="yxp-wordList-box-item">
          <div class="yxp-wordList-main">
            <div
              class="yxp-wordList-item list-item"
              v-for="(item, i) in wordList"
              :key="item.id"
              :class="{ active: item.num > 0 && curIndex === i }"
              @click="handlePlay(item, i)"
            >
              <div class="yxp-wordList-item-l">{{ item.word }}</div>
              <div
                class="yxp-wordList-item-r"
                v-if="item.num === 2 && curIndex === i"
              >
                {{ item.translate }}
              </div>
            </div>
            <span
              class="yxp-wordList-page-btn btn-hover"
              id="word-list-more:btn"
              v-show="wordList.length >= 20 && !isFinish"
              @click="handleMoreClick"
              >加载更多</span
            >
          </div>
        </div>
      </div>
    </div>
    <div class="yxp-word-box" id="wordBox" v-if="isStudy && checkIndex === 1">
      <div class="yxp-wordList-box">
        <div class="yxp-wordList-box-item">
          <div class="yxp-wordList-main">
            <div
              class="yxp-wordList-item list-item"
              v-for="(item, i) in wordList"
              :key="item.id"
              :class="{ active: item.num > 0 && curIndex === i }"
              @click="handlePlay(item, i)"
            >
              <div class="yxp-wordList-item-l">{{ item.word }}</div>
              <div
                class="yxp-wordList-item-r"
                v-if="item.num === 2 && curIndex === i"
              >
                {{ item.translate }}
              </div>
              <span
                class="yxp-wordList-page-btn btn-hover"
                id="word-list-more:btn"
                v-show="wordList.length >= 20 && !isFinish"
                @click="handleMoreClick"
                >加载更多</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 
    <word-list :dataList="wordList" v-if="!isStudy && wordList.length > 0"></word-list>
    <word-list :dataList="wordList" v-if="isStudy && wordList.length > 0 && checkIndex === 1"></word-list> -->
    <div
      class="yxp-word-box"
      id="wordBox"
      v-if="isStudy && wordList.length > 0 && checkIndex === 0"
    >
      <div class="yxp-wordList-box">
        <div class="yxp-wordList-box-item">
          <div class="yxp-wordList-main">
            <div
              class="yxp-wordList-item list-item"
              v-for="(item, i) in wordList"
              :key="item.id"
              @click="handlePlay(item, i)"
            >
              <div
                class="yxp-wordList-item-l"
                :class="{ active: item.num > 0 && curIndex === i }"
              >
                <span class="word-en">{{ item.word }}</span>
                <span
                  class="word-zh"
                  v-show="item.num === 2 && curIndex === i"
                  >{{ item.translate }}</span
                >
              </div>
              <div class="yxp-wordList-item-r">
                <div
                  class="success img1"
                  v-show="item.isSuccess"
                  @click.stop="handleSelectSuccess(i, item)"
                ></div>
                <div
                  class="success img2"
                  v-show="!item.isSuccess"
                  @click.stop="handleSelectSuccess(i, item)"
                ></div>
                <div
                  class="success img3"
                  v-show="!item.isError"
                  @click.stop="handleSelectError(i, item)"
                ></div>
                <div
                  class="success img4"
                  v-show="item.isError"
                  @click.stop="handleSelectError(i, item)"
                ></div>
              </div>
            </div>
            <span
              class="yxp-wordList-page-btn btn-hover"
              id="word-list-more:btn"
              v-show="wordList.length >= 20 && !isFinish"
              @click="handleMoreClick"
              >加载更多</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- <Study :data="wordList" v-if="isStudy && wordList.length > 0 && checkIndex === 0" @check="handleCheckResult" /> -->
    <Empty v-if="isNone" />
    <div class="yxp-familiar-pop" v-if="isShow">
      <div class="close" @click="handleClosed"></div>
      <!-- <img src="@/assets/word/close-icon.png" class="close" @click="handleClosed"/> -->
      <div class="code">
        <span>教学码</span>
        <input type="password" v-model="code" placeholder="请输入教学码" />
      </div>
      <div class="btn">
        <span class="btn-item" id="yxp-code-sure" @click="handleSaveCode"
          >确定</span
        >
        <span class="btn-item" id="yxp-code-reset" @click="handleClosed"
          >取消</span
        >
      </div>
    </div>
    <div class="mark" v-if="isShow || isBack || isCheck"></div>
    <div class="yxp-wordList-pop" v-if="isBack">
      <div class="close" id="yxp-code-back" @click="isBack = false"></div>
      <!-- <img src="@/assets/word/close-icon.png" class="close" @click="isBack = false"/> -->
      <p class="tips">是否要放弃筛查</p>
      <div class="btn">
        <span class="btn-item" id="yxp-f-sure" @click="handleSure">确定</span>
        <span class="btn-item" id="yxp-f-reset" @click="isBack = false"
          >取消</span
        >
      </div>
    </div>
    <div class="yxp-wordList-pop check-pop" v-if="isCheck">
      <div class="close" @click="isCheck = false"></div>
      <!-- <img src="@/assets/word/close-icon.png" class="close" @click="isCheck = false"/> -->
      <img src="@/assets/word/word-review.png" class="tips-img" />
      <p class="tips">您还没有可筛查的单词， 继续学习吧~</p>
      <div class="btn">
        <span class="btn-item" id="yxp-f-start" @click="handleStart"
          >继续识记</span
        >
      </div>
    </div>
    <audio ref="audio"></audio>
      <Timetips ref="timetips"></Timetips>
  </div>
</template>
<script>
import Empty from "@/components/Empty.vue";
import {
  squareWord,
  validationCode,
  screenWordsSubmit,
} from "@/services/word.js";
    import Timetips from "@/components/Timetips";
export default {
  name: "familiar",
  components: {
    // wordList,
    // Study,
    Empty,
    Timetips
  },
  data() {
    return {
      tabIndex: 1,
      curIndex: 0,
      checkIndex: 0,
      package_id: 0,
      square_type: 0,
      code: "",
      type: "complete",
      complete: true, // 已会
      wordList: [],
      tab: ["熟词", "已会", "难词"],
      check: ["未筛查", "已筛查"],
      isShow: false, // 教学码弹窗
      isStudy: false, // 筛查
      isBack: false,
      isCheck: false,
      text: "开始筛查", // 开始筛查按钮默认文案
      teachId: -1,
      canArr: [],
      canNotArr: [],
      isNone: false,
      pageIndex: 1,
      isFinish: false,
      is_doubleminus:sessionStorage.getItem('doubleminus_type')
    };
  },
  created() {
    if (this.$route.query) {
      const query = this.$route.query;
      this.package_id = query.package_id;
      this.square_type = query.square_type;
    }

    this.getWordList();
  },
  mounted() {
    // window.addEventListener('scroll', this.handleScroll,true)
  },
  beforeDestroy() {
    this.$refs.timetips.stopTimer();
    this.$refs.timetips.stopEndTimer();
  },
  methods: {
    async getWordList() {
      this.isFinish = true;
      this.$loading.show();
      let params = {
        square_type: this.square_type,
        package_id: this.package_id,
        type: this.type,
        page: this.pageIndex,
      };
      let data = await squareWord(params);
      if (data.code === 1) {
        this.$loading.hide();
        if (data.data.length === 0) {
          if (this.pageIndex === 1) {
            this.isNone = true;
          } else {
            this.isFinish = true;
            // window.removeEventListener('scroll', this.handleScroll,true)
          }
          return;
        }
        data.data.map((item) => {
          item.num = 0;
          item.isSuccess = false;
          item.isError = false;
        });
        this.wordList = [...this.wordList, ...data.data];
        this.$nextTick(() => {
          if (this.wordList.length < 7) {
            this.isFinish = true;
          } else {
            this.isFinish = false;
          }
        });
      } else if (data.code === "10001" || data.code === 10001) {
        this.$loading.hide();
        this.$toast("token过期，请重新登录");
      } else {
        this.$loading.hide();
        this.$toast(data.info);
      }
    },
    // 切换tab
    handleTab(i) {
      if (i === this.tabIndex) return;
      this.isNone = false;
      this.isStudy = false;
      this.tabIndex = i;
      this.pageIndex = 1;
      this.isFinish = false;
      this.wordList = [];
      this.canArr = [];
      this.canNotArr = [];
      this.isFinish = false;
      let scrollDom = document.getElementById("wordBox");
      scrollDom.scrollTop = 0;
      switch (i) {
        case 0:
          this.type = "easy";
          break;
        case 1:
          this.type = "complete";
          this.complete = true;
          this.checkIndex = 0;
          if (this.text === "提交") {
            this.isStudy = true;
          }
          break;
        case 2:
          this.type = "difficult ";
          break;
        default:
          break;
      }
      this.getWordList();
    },
    handleCheck(i) {
      if (i === this.checkIndex) return;
      this.checkIndex = i;
      this.pageIndex = 1;
      this.isFinish = false;
      this.wordList = [];
      this.canArr = [];
      this.canNotArr = [];
      this.isFinish = false;
      this.isNone = false;
      if (i === 0) {
        this.type = "complete";
      } else if (i === 1) {
        this.type = "done";
      }
      this.getWordList();
    },
    // 开始筛查
    handleStartCheck() {
      if (this.teachId === -1) {
        if (this.wordList.length === 0) {
          this.isCheck = true;
          return;
        }
        this.isShow = true;
      } else {
        // this.$router.push({
        //       path: '/wordRemember/wordGrade',
        //       query: {
        //         package_id: this.package_id,
        //         title: this.$route.query.title,
        //         path: window.encodeURIComponent('https://newyxp.sse100.com/index/index/certificate?from=2&complete=3&chapter_id=119&student_id=32045&type=3')
        //       }
        //     })

        this.screenSave();
      }
    },
    async screenSave() {
      this.$loading.show();
      let params = {
        can: this.canArr.join(","),
        can_not: this.canNotArr.join(","),
        package_id: this.package_id,
        teacher: this.teachId,
        is_first: "0",
      };
      let data = await screenWordsSubmit(params);
      if (data.code === 1) {
        this.$loading.hide();
        if (data.data.is_finish === 1) {
          this.$router.push({
            path: "/wordRemember/wordGrade",
            query: {
              path: window.encodeURIComponent(data.data.url),
              package_id: this.package_id,
              title: this.$route.query.title,
            },
          });
        } else {
          this.$router.replace({
            path: "/wordRemember",
          });
        }
      } else if (data.code === "10001" || data.code === 10001) {
        this.$loading.hide();
        this.$toast("token过期，请重新登录");
      } else {
        this.$loading.hide();
        this.$toast(data.info);
      }
    },
    //教学码保存
    async handleSaveCode() {
      this.$loading.show();
      let params = {
        code: this.code,
        type: 2,
      };
      let data = await validationCode(params);
      if (data.code === 1) {
        this.$loading.hide();
        this.isShow = false;
        this.isStudy = true;
        this.teachId = data.data;
        this.text = "提交";
      } else if (data.code === "10001" || data.code === 10001) {
        this.$loading.hide();
        this.$toast("token过期，请重新登录");
      } else {
        this.$loading.hide();
        this.$toast(data.info);
      }
    },
    // handleCheckResult (val) {
    //   if (val.type === 1) {
    //     this.successNum += 1
    //     if (this.canArr.indexOf(val.id) === -1) {
    //       this.canArr.push(val.id)
    //     }
    //     if (this.canNotArr.indexOf(val.id) > -1) {
    //       let index = this.canNotArr.indexOf(val.id)
    //       this.canNotArr.splice(index, 1)
    //     }
    //   } else if (val.type === 0) {
    //     this.errorNum += 1
    //     if (this.canNotArr.indexOf(val.id) === -1) {
    //       this.canNotArr.push(val.id)
    //     }
    //     if (this.canArr.indexOf(val.id) > -1) {
    //       let index = this.canArr.indexOf(val.id)
    //       this.canArr.splice(index, 1)
    //     }
    //   }
    // },
    handleSelectSuccess(i, item) {
      if (item.isSuccess) return;
      let obj = this.wordList[i];
      this.$set(obj, "isError", false);
      this.$set(obj, "isSuccess", true);
      if (this.canArr.indexOf(item.id) === -1) {
        this.canArr.push(item.id);
      }
      if (this.canNotArr.indexOf(item.id) > -1) {
        let index = this.canNotArr.indexOf(item.id);
        this.canNotArr.splice(index, 1);
      }
    },
    handleSelectError(i, item) {
      if (item.isError) return;
      let obj = this.wordList[i];
      this.$set(obj, "isSuccess", false);
      this.$set(obj, "isError", true);
      if (this.canNotArr.indexOf(item.id) === -1) {
        this.canNotArr.push(item.id);
      }
      if (this.canArr.indexOf(item.id) > -1) {
        let index = this.canArr.indexOf(item.id);
        this.canArr.splice(index, 1);
      }
    },
    // 播放语音
    handlePlay(item, i) {
      this.curIndex = i;
      let num = item.num;
      num++;
      if (num > 2) num = 1;
      this.$set(item, "num", num);
      let audio = this.$refs.audio;
      audio.src = item.project_voice;
      audio.play();
      this.wordList.map((elem, index) => {
        if (index !== i) {
          this.$set(elem, "num", 0);
        }
      });
    },
    handleScroll() {
      console.log(this.isFinish + "滚动");
      let scrollDom = document.getElementById("wordBox");
      if (scrollDom.scrollTop > 0) {
        if (
          scrollDom.clientHeight + parseInt(scrollDom.scrollTop) ==
          scrollDom.scrollHeight
        ) {
          if (!this.isFinish) {
            this.pageIndex++;
            this.isFinish = true;
            console.log(this.isFinish + "分页");
            this.getWordList();
          }
        }
      }
    },
    handleMoreClick() {
      if (!this.isFinish) {
        this.pageIndex++;
        this.isFinish = true;
        console.log(this.isFinish + "分页");
        this.getWordList();
      }
    },
    //关闭弹窗
    handleClosed() {
      this.isShow = false;
    },
    // 返回
    handleBack() {
      if(this.is_doubleminus){
        this.$router.replace({
          path: "/wordRemember",
        });
      }else {
        this.isBack = true;
      }
    },
    handleSure() {
      this.$router.replace({
        path: "/wordRemember",
      });
    },
    handleStart() {
      this.$router.replace({
        path: "/wordRemember",
      });
    },
  },
};
</script>
<style lang="less" scoped>
.yxp-wordList-top {
  justify-content: center;
  // .back {
  //   position: absolute;
  //   left: 40px;
  //   top: 30px;
  //   width: 30px;
  //   height: 27px;
  //   span {
  //     display: block;
  //     width: 30px;
  //     height: 27px;
  //     background: url(../../../assets/word/back-icon.png) no-repeat;
  //     background-size: 100%;
  //    cursor: pointer;
  //   }
  // }
  .yxp-top-c {
    display: flex;
    justify-content: center;
    align-items: center;
    .active {
      position: relative;
      color: #27bfa3;
      &::after {
        display: block;
        content: "";
        position: absolute;
        left: 50%;
        bottom: 8px;
        transform: translateX(-50%);
        width: 23px;
        height: 4px;
        background: #27bfa3;
        border-radius: 2px;
      }
      &:hover {
        color: #27bfa3;
      }
    }
    span {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #c7c7c7;
      margin-right: 50px;
      cursor: pointer;
      &:hover {
        color: #7e7e7e;
      }
    }
  }
  .yxp-top-r {
    height: 80px;
    line-height: 80px;
    .start-btn {
      width: 130px;
      height: 46px;
      line-height: 46px;
      text-align: center;
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #fff;
      background: linear-gradient(
        360deg,
        rgba(71, 203, 179, 1) 0%,
        rgba(81, 211, 176, 1) 100%
      );
      box-shadow: 0px 5px 10px rgba(59, 201, 164, 0.3);
      border-radius: 30px;
      margin-right: 50px;
      cursor: pointer;
      &:hover {
        background: linear-gradient(
          360deg,
          rgba(39, 178, 152, 1) 0%,
          rgba(52, 203, 163, 1) 100%
        );
        box-shadow: 0px 5px 10px rgba(59, 201, 164, 0.3);
      }
    }
    .check-btn {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .active {
        color: #fff;
        background: #27bfa3;
      }
      span {
        display: block;
        width: 100px;
        height: 46px;
        line-height: 42px;
        text-align: center;
        font-size: 20px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #27bfa3;
        background: rgba(255, 255, 255, 1);
        border: 2px solid rgba(39, 191, 163, 1);
        border-radius: 0px 25px 25px 0px;
        cursor: pointer;
        &:first-child {
          border-radius: 25px 0 0 25px;
          border-right: none;
        }
      }
    }
  }
}

.check-pop {
  height: 398px !important;
  .tips-img {
    display: inline-block;
    width: 190px;
    height: 190px;
    margin: 25px auto 0;
  }
  .tips {
    padding: 0 30px;
  }
  .btn {
    margin-top: 30px;
    &-item {
      width: 280px;
    }
  }
}
.yxp-familiar-pop {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 360px;
  height: 260px;
  background: #fff;
  border-radius: 10px;
  z-index: 1200;
  .close {
    position: absolute;
    right: 20px;
    top: 15px;
    width: 20px;
    height: 20px;
    background: url(../../../assets/word/close-icon.png) no-repeat;
    background-size: 20px 20px;
    cursor: pointer;
    &:hover {
      background-image: url(../../../assets/word/close-hover.png);
    }
  }
  .code {
    display: flex;
    justify-self: flex-start;
    align-items: center;
    text-align: left;
    border-bottom: 1px solid #dddddd;
    padding-top: 83px;
    margin-left: 40px;
    margin-right: 40px;
    span {
      display: block;
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #24272b;
      margin-right: 20px;
    }
    input {
      width: 150px;
      border: none;
      outline: none;
      font-size: 20px;
      font-family: Microsoft YaHei;
    }
  }
  .btn {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 50px;
    .btn-item {
      display: block;
      width: 130px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #fff;
      background: #27bfa3;
      border-radius: 100px;
      cursor: pointer;
      &:first-child {
        margin-right: 20px;
      }
      &:hover {
        background: #18a38a;
      }
    }
  }
}
::-webkit-input-placeholder {
  color: #cbcbcb;
}
</style>
